<template>
  <page-header-wrapper>
    <div class="page-card">
      <h-query hideTab :showKey="['startTime,endTime']" :query="query" @search="search">

      </h-query>
    </div>
    <a-card :bordered="false">
      <a-spin :spinning="loading">
        <a-empty style="margin-bottom: 20px" v-if="!config.dataSource.length"/>
        <div v-else class="event-list">
          <div :class="['event-item', item.isUnHandle ? 'un-handle' : '']" v-for="item in config.dataSource" :key="item.id">
            <div :class="['tag', 'tag' + item.handleType]">
              {{ item.handleTypeName }}
            </div>
            <div class="event-item-header">
              <div class="event-item-title">
                <a-tag color="#108ee9" v-if="item.isExistImg" @click="openImage(item)">
                  截图
                </a-tag>
                <a-tag color="#108ee9" v-if="item.isExistVideo" @click="openVideo(item)">
                  视频
                </a-tag>
                <span v-html="item.title" @click="toLink(item)" style="cursor:pointer;"></span>
              </div>
            </div>
            <div class="event-item-desc">{{ item.content }}</div>
            <div class="event-item-footer">
              <div class="event-item-footer-left">
                <div class="event-item-time">{{ item.procTime }}</div>
                <div class="event-item-address">{{ item.areas }}</div>
                <div class="event-item-address" style="color: #00a0e9">{{ item.type }}</div>
                <div class="event-item-address">{{ item.departNames }}</div>
                <div class="event-item-status" v-if="item.isFeedback === 2">已协同反馈</div>
              </div>
              <div class="event-item-footer-right">
                <a-button
                    :loading="item.editLoading"
                    :type="item.isUnHandle ? 'primary' : 'info'" @click="edit(item)" size="small">反馈</a-button>
                <a-button
                    :loading="item.shareLoading"
                    :type="item.isUnHandle ? 'danger' : 'info'"
                    @click="share(item)" size="small">分享</a-button>

              </div>
            </div>
          </div>
        </div>
      </a-spin>
      <div style="display: flex;align-items: center;justify-content: center;" v-if="config.dataSource.length">
        <a-pagination
            v-model="config.pagination.current"
            :page-size-options="config.pagination.pageSizeOptions"
            :total="config.pagination.total"
            show-size-changer
            :page-size="config.pagination.pageSize"
            @change="onShowSizeChange"
            @showSizeChange="onShowSizeChange"
        >
          <template slot="buildOptionText" slot-scope="props">
            <span v-if="props.value !== '50'">{{ props.value }}条/页</span>
            <span v-if="props.value === '50'">全部</span>
          </template>
        </a-pagination>
      </div>
    </a-card>

    <a-modal v-model="showModal" title="情况反馈" @ok="submit" @cancel="showModal = false">
      <a-form-model layout="horizontal" ref="ruleForm" :model="form" :rules="rules" size="large">
        <a-row :gutter="100">
          <a-col :span="24">
            <a-form-model-item label="指令内容" prop="content">
              {{ form.content }}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="回复内容" prop="backContent">
              <a-textarea
                  :auto-size="{ minRows: 5 }"
                  size="large" v-model="form.backContent" placeholder="请输入回复内容"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label-position="top" label="附件上传" prop="file">
              <UploadImageMutil ref="UploadImageMutilRef"  type="button" :showUploadList="true" @change="getUrl" :length="1" />
            </a-form-model-item>
          </a-col>
        </a-row>

      </a-form-model>
    </a-modal>
    <a-modal ok-text="复制剪贴板" cancel-text="取消" @ok="copy" v-model="showModalShare" title="事件分享">
      <a-form-model layout="horizontal" v-bind="{
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
          }" ref="ruleForm" :model="shareForm" size="large">
        <a-row :gutter="100">
          <a-col :span="24">
            <a-form-model-item label="事件网址"> {{ shareForm.eventLinkUrl }}</a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="事件标题">{{ shareForm.eventTitle }} </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="事件摘要">{{ shareForm.eventContent }} </a-form-model-item>
          </a-col>
        </a-row>

      </a-form-model>
    </a-modal>
    <a-modal width="40%" :footer="null" v-model="imageModal" title="图片预览">
      <img width="100%" :src="imageUrl" fit="contain"/>
    </a-modal>
    <a-modal
        width="40%"
        title="视频预览"
        v-model="videoModal"
        footer=""
        @cancel="videoModal = false"
    >
      <video
          v-if="videoModal"
          :src="videoUrl"
          controls
          autoplay
          class="video"
          width="100%"></video>
    </a-modal>
  </page-header-wrapper>

</template>
<script>
import {
  informationSyncPageList,
  createInformationFeedback,
  queryInformationFeedback,
  queryEventShare
} from '@/api/serve';
import UploadImageMutil from "@/components/UploadImageMutil/UploadImageMutil.vue";

export default {
  components: {UploadImageMutil},
  data() {
    return {
      showModal: false,
      form: {
      },
      rules: {
        backContent: [
          { required: true, message: '请输入回复内容', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入指令内容', trigger: 'change' }
        ]
      },
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        keyword: '',
        status: ''
      },
      config: {
        dataSource: [],
        pagination: {
          pageSizeOptions: [ '10', '20', '30', '50'],
          current: 1,
          pageSize: 10,
          total: 0,
        }
      },
      query: [
        {
          key: 'keyword',
          type: 'input',
          label: '事项关键字'
        },
        {
          key: 'status',
          type: 'select',
          label: '状态',
          options: [{
            label: '全部',
            value: ''
          },
            {
              label: '已反馈',
              value: 2
            },
            {
              label: '未反馈',
              value: 1
            }]
        }
      ],
      showModalShare: false,
      shareForm: {},
      imageUrl: '',
      videoUrl: '',
      imageModal: false,
      videoModal: false,
      loading: false,
      editLoading: false,
      shareLoading: false,
      fileList: []
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList(params = {}) {
      this.loading = true
      Object.assign(this.queryParams, params);
      const handleType = {
        0: '处置事件',
        1: '应急事件',
        2: '观察事件'
      }
      informationSyncPageList(this.queryParams).then(res => {
        this.config.dataSource = res.data.records.map(item => {
          return {
            ...item,
            shareLoading: false,
            editLoading: false,
            isUnHandle: item.isFeedback !== 2,
            handleTypeName: handleType[item.handleType]
          }
        })
        this.config.pagination.total = res.data.total;
        this.loading = false
      }).catch(() => {
        this.loading = false
      });

    },
    search(params) {
      Object.assign(this.queryParams, params);
      this.getList(params);
    },
    onShowSizeChange(current, pageSize) {
      this.config.pagination.current = current;
      this.config.pagination.pageSize = pageSize;
      this.getList({
        pageNo: current,
        pageSize
      });
    },
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          createInformationFeedback({
            eventId: this.form.eventId,
            backContent: this.form.backContent,
            content: this.form.content,
            orderId: this.form.orderId,
            file: this.form.file
          }).then(res => {
            this.showModal = false
            this.$message.success('反馈成功');
            this.getList()
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    edit(record) {
      record.editLoading = true
      queryInformationFeedback({orderId: record.orderId}).then(res => {
        record.editLoading = false
        this.showModal = true
        this.$nextTick(() => {
          if (res.data) {
            this.form = {
              orderId: record.orderId,
              eventId: record.id,
              backContent: res.data ? res.data.backContent : '',
              content: res.data ? res.data.content : '',
              file: res.data ? res.data.file : ''
            }

          } else {
            this.form = {
              orderId: record.orderId,
              eventId: record.id,
              backContent: '',
              content: '',
              file: ''
            }
          }
          if (this.form.file) {
            this.$refs.UploadImageMutilRef.setFileList([
              JSON.parse(this.form.file)
            ])
          } else {
            this.$refs.UploadImageMutilRef.clear()
          }
        })
      }).catch(() => {
        record.editLoading = false
      });
    },
    getUrl (fileList, i) {
      const file = fileList[0]
      this.form.file = JSON.stringify({ name: file.name, url: file.imgUrl })
    },
    toLink(item) {
      if (item.link) {
        window.open(item.link)
      } else {
        this.$message.warning('暂无链接')
      }
    },
    openImage(item) {
      this.imageUrl = item.imgUrl
      this.imageModal = true
    },
    openVideo(item) {
      this.videoUrl = item.videoUrl
      this.videoModal = true
    },
    share(item) {
      const id = item.id
      item.shareLoading = true
      queryEventShare({eventId: id}).then(res => {
        item.shareLoading = false
        console.log(res)
        this.shareForm = res.data
        this.showModalShare = true
      }).catch(() => {
        item.shareLoading = false
      })
    },
    copy() {
      let copyContent = `
       事件网址
       ${this.shareForm.eventLinkUrl || ''}
       事件标题
       ${this.shareForm.eventTitle || ''}
       事件摘要
        ${this.shareForm.eventContent || ''}
      `
      this.$copyText(copyContent).then(() => {
        this.$message.success('复制成功');
      }, () => {
        this.$message.error('复制失败');
      });

    }
  }
};
</script>
<style lang="less">
.event-list {
  .event-item {
    margin: 10px 0;
    padding: 10px 30px;
    border: 1px solid #e8e8e8;
    position: relative;
    min-height: 100px;
    padding-left: 70px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .ant-btn-info {
      background-color: #e2e2e2;
      color: #999999;
    }
    &.un-handle {
      border-color: red;
      .tag {
        background-color: red !important;
      }
    }
    .tag {
      width: 100px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      background-color: #00A0E9;
      color: #ffffff;
      font-size: 12px;
      position: absolute;
      transform: rotate(-45deg);
      left: -28px;
      top: 10px;
    }
    .tag1 {
      background-color: rgb(203,183,56);
    }
    .tag2 {
      background-color: green;
    }
    .event-item-header {
      display: flex;
      align-items: center;
      .event-item-title {
        font-size: 16px;
        font-weight: 700;
        .a-button {

        }
      }
    }
    .event-item-desc {
      min-height: 20px;
      line-height: 18px;
      font-size: 12px;
      color: #999999;
      margin: 10rpx 0;
    }
    .event-item-footer {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .event-item-footer-left {
        display: flex;
        align-items: center;
        .event-item-time {
          font-size: 12px;
          color: #999999;
        }
        .event-item-address {
          font-size: 12px;
          color: green;
          margin: 0 10px;
        }
        .event-item-status {
          color: red;
          font-size: 12px;
        }
      }
      .event-item-footer-right {

      }
    }
  }
}
</style>
